package com.abc.cloud.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.abc.cloud.common.utils.UserHolder;
import com.abc.cloud.dto.im.ContactsSearch;
import com.abc.cloud.dto.im.ContactsUserDto;
import com.abc.cloud.dto.user.UserDto;
import com.abc.cloud.entity.UserEntity;
import com.abc.cloud.vo.im.AdminUpdateContactsReq;
import com.abc.cloud.vo.im.AdminUpdateGroupReq;
import com.abc.cloud.vo.im.ContactsUpdateReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.abc.cloud.entity.ContactsEntity;
import com.abc.cloud.service.ContactsService;
import com.abc.cloud.common.utils.PageUtil;
import com.abc.cloud.common.utils.Result;



/**
 * 联系人表
 *
 * @author LiJunXi
 * @email 2770063826@qq.com
 * @date 2023-11-29 13:19:41
 */
@Api(tags = "联系人接口")
@RestController
@RequestMapping("/contacts")
public class ContactsController {
    @Autowired
    private ContactsService contactsService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params){
        PageUtil page = contactsService.queryPage(params);

        return Result.success(page, "page");
    }


    /**
     * 信息
     */
    @ApiOperation("获取联系人信息")
    @GetMapping("/info/{id}")
    public Result<ContactsUserDto> info(@PathVariable("id") Long id){
		ContactsUserDto contacts = contactsService.info(id);

        return Result.success(contacts, "获取成功");
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody ContactsEntity contacts){
		contactsService.save(contacts);

        return Result.success(null, "");
    }

    /**
     * 修改
     */
    @ApiOperation("修改联系人信息")
    @RequestMapping("/update")
    public Result update(@RequestBody ContactsUpdateReq contactsUpdateReq){
        Long userId = UserHolder.getUser().getId();
		contactsService.updateContacts(userId, contactsUpdateReq.getId(), contactsUpdateReq.getGroupId(), contactsUpdateReq.getNote(), contactsUpdateReq.getStatus());

        return Result.success(null, "操作成功");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody Long[] ids){
		contactsService.removeByIds(Arrays.asList(ids));

        return Result.success(null, "");
    }

    /**
     * 搜索联系人/群号
     * @return
     */
    @ApiOperation("搜索联系人/群号")
    @PostMapping("/search")
    public Result<ContactsSearch> search(@RequestBody Map<String, String> data){
        String searchContent = data.get("searchContent");
        ContactsSearch userDtoList = contactsService.search(searchContent);

        return Result.success(userDtoList, "获取成功");
    }

    @ApiOperation("管理员分页获取联系人列表")
    @GetMapping("/admin/list")
    public Result adminGroupList(@RequestParam Map<String, Object> params){
        PageUtil page = contactsService.adminContactsList(params);

        return Result.success(page, "获取成功");
    }

    @ApiOperation("管理员修改联系人")
    @PutMapping("/admin/update")
    public Result adminUpdateGroup(@RequestBody AdminUpdateContactsReq adminUpdateContactsReq){
        contactsService.adminUpdateContacts(adminUpdateContactsReq);

        return Result.success(null, "操作成功");
    }

    @ApiOperation("管理员批量修改聊联系人状态")
    @PutMapping("/admin/updateStatus/{status}")
    public Result adminDeleteGroup(@RequestBody List<Long> ids,
                                   @PathVariable("status") Integer status){
        contactsService.adminDeleteContacts(ids, status);

        return Result.success(null, "操作成功");
    }

}
